-- 创建主框架
local targetTracker = CreateFrame("Frame", "TargetChangeTracker", UIParent)
targetTracker:SetSize(300, 120)
targetTracker:SetPoint("CENTER", 0, 150)

-- 设置背景
targetTracker.bg = targetTracker:CreateTexture(nil, "BACKGROUND")
targetTracker.bg:SetAllPoints(true)
targetTracker.bg:SetColorTexture(0.1, 0.1, 0.2, 0.8)

-- 设置边框
targetTracker.border = CreateFrame("Frame", nil, targetTracker, "BackdropTemplate")
targetTracker.border:SetPoint("TOPLEFT", -5, 5)
targetTracker.border:SetPoint("BOTTOMRIGHT", 5, -5)
targetTracker.border:SetBackdrop({
    edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
    edgeSize = 16,
})

-- 标题文本
targetTracker.title = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
targetTracker.title:SetPoint("TOP", 0, -15)
targetTracker.title:SetText("目标变化追踪器")
targetTracker.title:SetTextColor(1, 0.5, 0)

-- 目标名称文本
targetTracker.nameText = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
targetTracker.nameText:SetPoint("TOP", 0, -40)
targetTracker.nameText:SetText("无目标")

-- 目标类型文本
targetTracker.typeText = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
targetTracker.typeText:SetPoint("TOP", targetTracker.nameText, "BOTTOM", 0, -10)
targetTracker.typeText:SetText("")

-- 目标阵营文本
targetTracker.factionText = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
targetTracker.factionText:SetPoint("TOP", targetTracker.typeText, "BOTTOM", 0, -10)
targetTracker.factionText:SetText("")

-- 目标生命值文本
targetTracker.healthText = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
targetTracker.healthText:SetPoint("TOP", targetTracker.factionText, "BOTTOM", 0, -10)
targetTracker.healthText:SetText("")

-- 注册PLAYER_TARGET_CHANGED事件
targetTracker:RegisterEvent("PLAYER_TARGET_CHANGED")
targetTracker:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_TARGET_CHANGED" then
        self:UpdateTargetInfo()
    end
end)

-- 更新目标信息函数
targetTracker.UpdateTargetInfo = function(self)
    -- 重置文本
    self.nameText:SetText("无目标")
    self.typeText:SetText("")
    self.factionText:SetText("")
    self.healthText:SetText("")

    -- 检查目标是否存在
    if not UnitExists("target") then
        self.nameText:SetText("|cFF999999无目标|r")
        return
    end

    -- 获取目标基础信息
    local targetName = UnitName("target")
    local targetClass, classFilename = UnitClass("target")
    local targetLevel = UnitLevel("target")
    local targetReaction = UnitReaction("target", "player")
    local targetType = UnitCreatureType("target")

    -- 设置名称
    local nameColor = "|cFFFFFFFF"
    if UnitIsPlayer("target") then
        -- 玩家目标
        local classColor = RAID_CLASS_COLORS[classFilename] or {r=1, g=1, b=1}
        nameColor = format("|cFF%02x%02x%02x", classColor.r*255, classColor.g*255, classColor.b*255)
    end
    self.nameText:SetText(nameColor..targetName.." - |r"..format("|cFF99CCFF等级 %d|r", targetLevel))

    -- 设置类型
    if UnitIsPlayer("target") then
        self.typeText:SetText("类型: |cFF00FF00玩家|r ("..targetClass..")")
    elseif targetType then
        self.typeText:SetText("类型: |cFF00FFFF"..targetType.."|r")
    else
        self.typeText:SetText("类型: |cFFFF0000未知|r")
    end

    -- 设置阵营关系
    if UnitIsEnemy("player", "target") then
        self.factionText:SetText("阵营: |cFFFF0000敌对|r")
    elseif UnitIsFriend("player", "target") then
        self.factionText:SetText("阵营: |cFF00FF00友善|r")
    elseif UnitIsNeutral("target") then
        self.factionText:SetText("阵营: |cFFFFFF00中立|r")
    else
        self.factionText:SetText("阵营: |cFF999999未知|r")
    end

    -- 设置生命值
    local healthPercent = UnitHealth("target") / UnitHealthMax("target")
    local healthColor
    if healthPercent > 0.7 then
        healthColor = "|cFF00FF00"
    elseif healthPercent > 0.3 then
        healthColor = "|cFFFFFF00"
    else
        healthColor = "|cFFFF0000"
    end
    self.healthText:SetText(format("生命值: %s%.1f%%|r", healthColor, healthPercent * 100))
end

-- 添加拖动功能
targetTracker:SetMovable(true)
targetTracker:EnableMouse(true)
targetTracker:SetClampedToScreen(true)
targetTracker:RegisterForDrag("LeftButton")

targetTracker:SetScript("OnDragStart", targetTracker.StartMoving)
targetTracker:SetScript("OnDragStop", targetTracker.StopMovingOrSizing)

-- 初始更新
targetTracker:UpdateTargetInfo()

-- 添加使用说明
local helpText = targetTracker:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
helpText:SetPoint("BOTTOM", 0, 15)
helpText:SetText("|cFF999999左键拖动移动框架|r")

-- 打印说明
print("目标变化追踪器已加载!")
print(" - PLAYER_TARGET_CHANGED事件处理已启用")
print(" - 每次改变目标都会更新显示信息")
print(" - 按住左键可拖动框架位置")